package com.liuheng.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuheng.spzx.model.dto.h5.ProductSkuDto;
import com.liuheng.spzx.model.entity.product.Product;
import com.liuheng.spzx.model.entity.product.ProductDetails;
import com.liuheng.spzx.model.entity.product.ProductSku;
import com.liuheng.spzx.model.vo.h5.ProductItemVo;
import com.liuheng.spzx.product.mapper.ProductDetailsMapper;
import com.liuheng.spzx.product.mapper.ProductMapper;
import com.liuheng.spzx.product.mapper.ProductSkuMapper;
import com.liuheng.spzx.product.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper,ProductSku> implements ProductSkuService {
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper detailsMapper;
    @Override
    public Page<ProductSku> findPage(Page<ProductSku> productSkuPage, ProductSkuDto productSkuDto) {
        List<ProductSku> list=productSkuMapper.findPage(productSkuPage,productSkuDto);
        productSkuPage.setRecords(list);
        return productSkuPage;
    }

    @Override
    public ProductItemVo item(Long skuId) {
        ProductItemVo productItemVo = new ProductItemVo();
        ProductSku productSku = productSkuMapper.selectById(skuId);
        Product product=productMapper.selectById(productSku.getProductId());
        LambdaQueryWrapper<ProductDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(product.getId()!=null,ProductDetails::getProductId,product.getId());
        ProductDetails productDetails = detailsMapper.selectOne(wrapper);
        List<String> detailsImageUrlList = Arrays.stream(productDetails.getImageUrls().split(",")).collect(Collectors.toList());

        List<String> sliderulrList = Arrays.stream(product.getSliderUrls().split(",")).collect(Collectors.toList());
        LambdaQueryWrapper<ProductSku> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(productSku!=null,ProductSku::getProductId,productSku.getProductId());
        List<ProductSku> productSkuList = productSkuMapper.selectList(wrapper1);
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setDetailsImageUrlList(detailsImageUrlList);
        productItemVo.setSliderUrlList(sliderulrList);
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        return productItemVo;
    }
}
